package org.chen.ctrip.controller;// CrawlerTaskController.java
import io.swagger.v3.oas.annotations.tags.Tag;
import org.chen.common.ctrip.crawler.CrawlerTask;
import org.chen.common.ctrip.crawler.CrawlerTaskLog;
import org.chen.common.result.ResultVO;
import org.chen.ctrip.scheduler.CrawlerScheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/crawler-tasks")
@Tag(name = "不重要的接口", description = "不重要的接口")
public class CrawlerTaskController {

    @Autowired
    private CrawlerScheduler crawlerScheduler;

    @GetMapping
    public ResultVO getAllTasks() {
        try {
            List<CrawlerTask> tasks = crawlerScheduler.getScheduledTasks();
            return ResultVO.success(tasks);
        } catch (Exception e) {
            return ResultVO.fail("获取任务列表失败: " + e.getMessage());
        }
    }

    @PostMapping
    public ResultVO createTask(@RequestBody CrawlerTask task) {
        try {
            crawlerScheduler.addScheduledTask(task);
            return ResultVO.success("任务创建成功");
        } catch (Exception e) {
            return ResultVO.fail("任务创建失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ResultVO updateTask(@PathVariable Long id, @RequestBody CrawlerTask task) {
        try {
            task.setId(id);
            CrawlerTask crawlerTask = crawlerScheduler.updateScheduledTask(task);
            return ResultVO.success("任务更新成功", crawlerTask);
        } catch (Exception e) {
            return ResultVO.fail("任务更新失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResultVO deleteTask(@PathVariable Long id) {
        try {
            crawlerScheduler.deleteScheduledTask(id);
            return ResultVO.success("任务删除成功");
        } catch (Exception e) {
            return ResultVO.fail("任务删除失败: " + e.getMessage());
        }
    }

    @PostMapping("/{id}/pause")
    public ResultVO pauseTask(@PathVariable Long id) {
        try {
            crawlerScheduler.toggleScheduledTask(id, "pause");
            return ResultVO.success("任务已暂停");
        } catch (Exception e) {
            return ResultVO.fail("暂停任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/{id}/resume")
    public ResultVO resumeTask(@PathVariable Long id) {
        try {
            crawlerScheduler.toggleScheduledTask(id, "resume");
            return ResultVO.success("任务已恢复");
        } catch (Exception e) {
            return ResultVO.fail("恢复任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/{id}/execute")
    public ResultVO executeTaskNow(@PathVariable Long id) {
        try {
            crawlerScheduler.executeScheduledTaskNow(id);
            return ResultVO.success("任务执行已启动");
        } catch (Exception e) {
            return ResultVO.fail("执行任务失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}/logs")
    public ResultVO getTaskLogs(@PathVariable Long id, @RequestParam(defaultValue = "50") int limit) {
        try {
            List<CrawlerTaskLog> logs = crawlerScheduler.getTaskLogs(id, limit);
            return ResultVO.success(logs);
        } catch (Exception e) {
            return ResultVO.fail("获取任务日志失败: " + e.getMessage());
        }
    }
}